// pages/firstpage/firstpage.js
import { Firstpage } from '../../model/firstpage.js'
const firstpage = new Firstpage();
import { Onlineclass } from '../../model/onlineclass.js'
const onlineclass = new Onlineclass();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentHeight:'',
    id:'',
    // 轮播
    imgUrls: [],
    indicatorDots: false,
    autoplay: true,
   
    swiperCurrent: 0,
    // tab
    categoryMenu: [],
    category:[],
    categoryCur: 0,
    one_text:"",
    unionid: '',
    // 内容
    // currentTab:0,
    propage:0,
    itemlist:[],
    itemcount: 0,
    itemnewcount:0,
    isnohave:true,
    loging:'没有更多了',
    index:0,
    duration:1000,
    interval:2000,
    showEmpty:false,
    isShow:0,
  },

  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.banners()
    this.one_word()
    this.setData({
      itemlist: [],
      propage:0,
      itemcount: 0,
      itemnewcount: 0,
    })
   
    var index = this.data.categoryCur;
    this.union_list(index);
    //首页高度
    this.homePageHeight();
    this.getShowOrHide()
  },
  // 查看隐藏或展示
  getShowOrHide(){
    var that=this
    onlineclass.getShowOrHide().then(res=>{
      console.log(res)
      that.setData({
        isShow:res.data.isShow
      })
    }).catch(error=>{
      console.log(error)
    })
  },
  //首页高度
  homePageHeight(){
    var that = this;
    wx.getSystemInfo({
      complete: (res) => {
        that.setData({
          currentHeight:Number(res.windowHeight)
        })
      },
    })
  },


  // 顶部tab切换事件
  toggleCategory(e) {
    console.log(e);
    const that = this;
    that.setData({
      categoryCur: e.detail.index,
      isDoubleClick: e.detail.isDoubleClick,
      currentTab: e.detail.index,
      unionid: this.data.categoryMenu[e.detail.index].id,
      propage: 0,
      itemlist: [],
      itemcount: 0,
      itemnewcount: 0,
      loging:'没有更多了'
    });
    console.log("参数" + that.data.categoryCur);
    that.items_list()
    if (that.data.isDoubleClick == "1") {
      console.log('添加双击事件')
    }

  },
//轮播图-方法
Img_src_click(e){
  console.log(e)
  var imgArr = [];
  for (var i in this.data.imgUrls) {
    imgArr.push(this.data.imgUrls[i].picUrl)
    // if (this.data.imgUrls[i].picType != 2) {
    // imgArr.push(this.data.imgUrls[i].picUrl)
    // }
  }
  wx.previewImage({
    current: e.currentTarget.dataset.item.picUrl,
    urls: imgArr,
    success: function (res) { },
    fail: function (res) { },
    complete: function (res) { },
  })


},
  // 首页banner
  banners(){
    var that = this
    firstpage.banner_list()
    .then(res=>{
      console.log(res)
      if(res.code == 0){
        that.setData({
          imgUrls: res.data.datums
        })
      }
    })
    .catch(error=>{
      console.log('首页banner接口')
    })
  },
  //轮播图轮滑方向
  imageChange:function(e){
    var that = this;
    console.log(e)
    var index = e.detail.current;
    var length =that.data.imgUrls.length;
    console.log(index);
    if(index == 0) {
        that.setData({
            duration:1000,
            interval:3000
        });
    }    
    if(index > length-1) {  
        index = 0;     
        that.setData({
          duration:1,
          interval:100,
          index:index,
        });
    }
  },
  //get每日一句话
  one_word(){
    
    firstpage.one_word().then(res=>{
      console.log(res)
      this.setData({
        one_text:res.data.one_word
      })
    })
    .catch(error=>{

    })
  },
  //工会标签
  union_list(index){
    firstpage.union_list().then(res=>{
      
      var list=[]
      for(var i=0;i<res.data.datums.length;i++){
       list.push(res.data.datums[i].name)
      //  this.setData({
      //    id:res.data.datums[i].id
      //  })
      }
      this.setData({
        category:list,
        categoryMenu:res.data.datums,
        unionid: res.data.datums[index].id,

      })
      this.items_list()
      console.log(res)
    }).catch(err=>{
      console.log(err)
    })
  },
  
  // 轮播图变化
  swiperChange(e) {
    let current = e.detail.current;
    // console.log(current, '轮播图')
    let that = this;
    that.setData({
      swiperCurrent: current,
    })
  },
  // 切换
  // switchTab(event) {
  //   this.setData({
  //     categoryCur: event.detail.current,
  //     currentTab: event.detail.current,
  //     unionid: this.data.categoryMenu[event.detail.current].id,
  //     propage: 0,
  //     itemlist: [],
  //     itemcount: 0,
  //     itemnewcount: 0,
  //   });
  //   this.items_list()
  // },
  // 标签下的项目
  items_list(){
    var obj = {
      unionId: this.data.unionid,
      page:this.data.propage,
    }
    firstpage.items_list(obj)
    .then(res=>{
      if(res.code ==0){
        var itemlist = this.data.itemlist
        for (var i in res.data.datums){
          itemlist.push(res.data.datums[i])
        }
        this.setData({
          itemlist: itemlist,
          itemcount: res.data.count,
          itemnewcount: itemlist.length,
          isnohave:true,
        })
        if(itemlist.length==0){
          this.setData({
            showEmpty:true,
            showTips:'本工会暂时还没有活动~'
          })
        }else{
          this.setData({
            showEmpty:false
          })
        }

      }else if(res.code ==1){
        if(res.msg =='您的权限不足，暂时无法查看'){
          this.setData({
            showEmpty:true,
            showTips:'您的权限不足，暂时无法查看'
          })
          wx.showToast({
            title: res.msg,
            icon:'none'
          })
        }
      }



      
      console.log(res)
    })
    .catch(error=>{
      console.log(error);
      this.setData({
        showEmpty:true
      })
      if(res.msg == '您的权限不足，暂时无法查看'){
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
     
      this.setData({
        isnohave:false
      })
    })
  },
  // 去活动详情
  tomsg(e){
    console.log(e)
    console.log(e.currentTarget.dataset)
    if(e.currentTarget.dataset.item.classType == 1){
      wx.navigateTo({
        url: '/pages/activity_signup/activity_signup?id=' + e.currentTarget.dataset.item.classId,
      })
    }else if(e.currentTarget.dataset.item.classType == 2){
      wx.navigateTo({
        url: '/pages/activity_video/activity_video?id=' + e.currentTarget.dataset.item.classId,
      })
    }else if(e.currentTarget.dataset.item.classType == 3){
      wx.navigateTo({
        url: '/pages/vote/vote?id=' + e.currentTarget.dataset.item.classId,
      })
    }else if(e.currentTarget.dataset.item.classType == 4){
      wx.navigateTo({
        url: '/pages/dynamic/dynamic?id=' + e.currentTarget.dataset.item.classId,
      })
    }else if(e.currentTarget.dataset.item.classType == 5){
      wx.navigateTo({
        url: '/pages/inquiry/inquiry?id=' + e.currentTarget.dataset.item.classId,
      })
    }else if(e.currentTarget.dataset.item.classType == 6){
      wx.navigateTo({
        url: '/pages/stepnum/stepnum?id=' + e.currentTarget.dataset.item.classId,
      })
    }else if(e.currentTarget.dataset.item.classType == 7){
      wx.navigateTo({
        url: '/pages/location_signin/location_signin?id=' + e.currentTarget.dataset.item.classId,
      })
    }else if(e.currentTarget.dataset.item.classType == 8){
      wx.navigateTo({
        url: '/pages/share/share?id=' + e.currentTarget.dataset.item.classId,
      })
    }
  },
  // 跳转html5
  tohtml(e){
    console.log(e)
    var id = e.currentTarget.dataset.item.id;
    var type = e.currentTarget.dataset.item.type;
    if (type == 2) {
      wx.navigateTo({
        url: '/pages/html5/html5?type=1&id=' + id,
      })
    } 
  },


  //下拉刷新
  onPulling(e) {
    console.log('onPulling:', e)
    },
    
    onRefresh() {
    if (this._freshing) return
    this._freshing = true
      this.banners()
      this.one_word()
      // this.union_list(this.data.categoryCur);
      this.setData({
        triggered: false,
        })
        this._freshing = false
    },
    
    onRestore(e) {
    console.log('onRestore:', e)
    },
    
    onAbort(e) {
    console.log('onAbort', e)
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // // var pages = getCurrentPages(); //当前页面
    // // var beforePage = pages[pages.length - 2]; //前一页
    // // console.log(beforePage)
    // // console.log(pages)
    // if (this.data.isnohave == false){
    //   this.setData({
    //     categoryCur:0,
    //     isnohave:true
    //   })
    //   var index = 0;
    //   this.union_list(index)
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  bindscrolltolower(e){
    console.log("底部")
    console.log(e)
    if (Number(this.data.itemcount)> Number(this.data.itemnewcount)){
      console.log('分页');
      this.setData({
        propage: this.data.propage + 1,
        loging:'加载中...'
        
      });
      this.items_list()
    }else{
      console.log("else")
      this.setData({
        loging:'没有更多了'
      })
    }
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    



    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})